package com.wanlian.router.ui.me;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import com.smart.android.ui.tools.MyNiuBAdapter;
import com.wanlian.router.R;
import com.wanlian.router.bean.NetDevice;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Hyu on 2018/9/8.
 * Email: fvaryu@163.com
 */
public class MyDeviceAdapter extends MyNiuBAdapter<NetDevice> {

    private OnSwitchButtonClickListener mOnSwitchButtonClickListener;
    private OnDeleteButtonClickListener mOnDeleteButtonClickListener;

    public void setOnDeleteButtonClickListener(OnDeleteButtonClickListener onDeleteButtonClickListener) {
        mOnDeleteButtonClickListener = onDeleteButtonClickListener;
    }

    public void setOnSwitchButtonClickListener(OnSwitchButtonClickListener onSwitchButtonClickListener) {
        mOnSwitchButtonClickListener = onSwitchButtonClickListener;
    }

    public interface OnDeleteButtonClickListener {
        void onDeleteButtonClick(NetDevice device);
    }

    public interface OnSwitchButtonClickListener {
        void onSwitchButtonClick(NetDevice device);
    }

    public MyDeviceAdapter(Context context, List<NetDevice> mTs) {
        super(context, mTs);
    }

    @Override
    public int layout() {
        return R.layout.layout_item_mydevice;
    }

    @Override
    public MyViewHolder<NetDevice> holder(View view) {
        return new DeviceHolder(view);
    }

    class DeviceHolder extends MyViewHolder<NetDevice> {

        @BindView(R.id.tv_name)
        TextView mName;
        @BindView(R.id.tv_status)
        TextView mStatus;
        @BindView(R.id.image_status)
        ImageView mImageView;
        @BindView(R.id.tv_switch)
        TextView mSwitch;
        @BindView(R.id.btnDelete)
        Button delete;
        @BindView(R.id.swip)
        SwipeMenuLayout mSwipeRefreshLayout;

        public DeviceHolder(View view) {
            super(view);
        }

        @OnClick(R.id.btnDelete)
        void delete(View view) {
            if (view.getTag() == null || mOnDeleteButtonClickListener == null) {
                return;
            }

            mOnDeleteButtonClickListener.onDeleteButtonClick((NetDevice) view.getTag());
        }

        @Override
        public void setData(int i, NetDevice netDevice) {
            mSwipeRefreshLayout.quickClose();
            mImageView.setImageResource(netDevice.isOnline() ?
                    R.drawable.ic_router_online : R.drawable.ic_router_offline);
            mName.setText(netDevice.getDeviceName());
            mStatus.setText(netDevice.isOnline() ? "在线" : "离线");


            delete.setTag(netDevice);

            if (netDevice.isCurrent()) {
                mSwitch.setTag(null);
                mSwitch.setBackgroundResource(0);
                mSwitch.setText("当前设备");
                mSwitch.setTextColor(ContextCompat.getColor(mStatus.getContext(), R.color.color_66));
            }else {
                mSwitch.setTag(netDevice);
                mSwitch.setBackgroundResource(R.drawable.drawable_bg_trans_blue_5);
                mSwitch.setText("切换设备");
                mSwitch.setTextColor(ContextCompat.getColor(mStatus.getContext(), R.color.color_blue));
            }

            mSwitch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (v.getTag() == null) {
                        return;
                    }
                    NetDevice net = (NetDevice) v.getTag();
                    if (mOnSwitchButtonClickListener != null) {
                        mOnSwitchButtonClickListener.onSwitchButtonClick(net);
                    }
                }
            });
        }
    }
}
